//
//  HJTabBarViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJTabBarViewController.h"
#import "HJNavigationViewController.h"
#import "HJHomeViewController.h"
#import "HJMineViewController.h"
#import "HJCategroyController.h"

@interface HJTabBarViewController ()

@end

@implementation HJTabBarViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [UINavigationBar appearance];
    self.tabBar.backgroundColor = HJMainColor;
    self.tabBar.viewForFirstBaselineLayout.backgroundColor = HJColorRGB(255);
    
    // 首页
    HJHomeViewController * homeVC = [[HJHomeViewController alloc]init];
    [self addChildVc:homeVC title:@"首页" image:@"Fill home" selectedImage:@"homes"];
    
    // 分类
    HJCategroyController * categroyVC = [[HJCategroyController alloc]init];
    [self addChildVc:categroyVC title:@"浏览" image:@"category" selectedImage:@"fenlei hui"];
    
    // 个人中心
    HJMineViewController * mineVC = [[HJMineViewController alloc]init];
    [self addChildVc:mineVC title:@"我的" image:@"minecenter" selectedImage:@"mine"];
    
    //移除顶部线条
    self.tabBar.backgroundImage = [UIImage new];
    self.tabBar.shadowImage = [UIImage new];
    
    //添加阴影
    self.tabBar.layer.shadowColor = HJColorRGB(0).CGColor;
    self.tabBar.layer.shadowOffset = CGSizeZero;
    self.tabBar.layer.shadowOpacity = 0.2;
}

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    // 先给外面传进来的小控制器 包装 一个导航控制器
    HJNavigationViewController *nav = [[HJNavigationViewController alloc] initWithRootViewController:childVc];
    nav.tabBarController.tabBar.backgroundColor = HJColorRGB(255);
    childVc.navigationItem.title = nil;
    // 设置子控制器的文字
    childVc.tabBarItem.title = title; // 设置tabbar的文字
    
    // 设置子控制器的图片
    if (iOS7) {
        childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childVc.tabBarItem.image = [UIImage imageNamed:image];
        childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = HJColorRGB(51);
    textAttrs[NSFontAttributeName] = HJFontSize(11);
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = HJColorRGB(0);
    selectTextAttrs[NSFontAttributeName] = HJFontSize(11);
    [nav.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    // 添加为子控制器
    [self addChildViewController:nav];
}

@end
